package cn.liontalk.common.redis;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import com.alibaba.fastjson.JSON;

/**
 * @author: 周哲
 * @package: cn.liontalk.common.redis
 * @description:
 * @date: 2019/1/18 17:07
 * @version: V1.0
 */
@Service
public class RedisService {


    @Autowired
    JedisPool jedisPool;


    /**
     * 获取某个值
     *
     * @param keyPrefix 前缀
     * @param key       键
     * @param clazz     类型
     * @param <T>       类型
     * @return 返回数据
     */
    public <T> T get(KeyPrefix keyPrefix, String key, Class <T> clazz) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            String realKey = keyPrefix.getPrefix() + key;
            String result = jedis.get(realKey);
            T t = stringToBean(result, clazz);
            return t;
        } finally {
            retrunToPool(jedis);
        }
    }

    /**
     * 设置某个值
     *
     * @param keyPrefix 前缀
     * @param key       键
     * @param value     值
     * @param <T>       类型
     * @return 返回数据
     */
    public <T> boolean set(KeyPrefix keyPrefix, String key, T value) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            if (null == key || key.length() <= 0) {
                return false;
            } else {
                String result = beanToJson(value);
                String realKey = keyPrefix.getPrefix() + key;
                jedis.set(realKey, result);
                return true;
            }
        } finally {
            retrunToPool(jedis);
        }
    }

    /**
     * 判断某个值是否存在
     *
     * @param keyPrefix 前缀
     * @param key       键
     * @param <T>       类型
     * @return 返回数据
     */
    public <T> boolean keyExist(KeyPrefix keyPrefix, String key) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            if (null == key || key.length() <= 0) {
                return false;
            } else {
                String realKey = keyPrefix.getPrefix() + key;
                return jedis.exists(realKey);
            }
        } finally {
            retrunToPool(jedis);
        }
    }

    /**
     * 给值计数增加
     *
     * @param keyPrefix 前缀
     * @param key       键
     * @return 返回增加后的数据
     */
    public <T> Long incr(KeyPrefix keyPrefix, String key) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            if (null == key || key.length() <= 0) {
                return null;
            } else {
                String realKey = keyPrefix.getPrefix() + key;
                return jedis.incr(realKey);
            }
        } finally {
            retrunToPool(jedis);
        }
    }


    /**
     * 删除某个值
     *
     * @param keyPrefix 前缀
     * @param key       键
     * @return 返回增加后的数据
     */
    public <T> Long del(KeyPrefix keyPrefix, String key) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            if (null == key || key.length() <= 0) {
                return null;
            } else {
                String realKey = keyPrefix.getPrefix() + key;
                return jedis.del(realKey);
            }
        } finally {
            retrunToPool(jedis);
        }
    }

    /**
     * 给值减少
     *
     * @param keyPrefix 前缀
     * @param key       键
     * @return 返回增加后的数据
     */
    public <T> Long decr(KeyPrefix keyPrefix, String key) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            if (null == key || key.length() <= 0) {
                return null;
            } else {
                String realKey = keyPrefix.getPrefix() + key;
                return jedis.decr(realKey);
            }
        } finally {
            retrunToPool(jedis);
        }
    }

    public static <T> String beanToJson(T value) {
        if (null == value) {
            return null;
        }
        Class <?> clazz = value.getClass();
        if (clazz == int.class || clazz == Integer.class) {
            return "" + value;
        } else if (clazz == String.class) {
            return (String) value;
        } else if (clazz == Long.class || clazz == long.class) {
            return "" + value;
        }
        return JSON.toJSONString(value);
    }

    public static  <T> T stringToBean(String result, Class <T> clazz) {
        if (result == null || result.length() <= 0 || clazz == null) {
            return null;
        }
        if (clazz == int.class || clazz == Integer.class) {
            return (T) Integer.valueOf(result);
        } else if (clazz == long.class || clazz == Long.class) {
            return (T) Long.valueOf(result);
        } else if (clazz == String.class) {
            return (T) result;
        } else {
            return JSON.toJavaObject(JSON.parseObject(result), clazz);
        }
    }

    private void retrunToPool(Jedis jedis) {
        if (null != jedis) {
            jedis.close();
        }
    }


}
